

import { DataTableAction, DataTableActionTypes } from '../actions';
import { DataTable } from '../../models';

export interface DataTableState {
    entities: DataTable[],
    lastAdd: DataTable,
    lastRemove: DataTable,
    visible: 'show'|'hide'|'half-hide',
    selectedIndex: number,
    mouseOver: { layer: string, id: string },
    mouseOut: { layer: string, id: string }
}

export const initialDataTableState: DataTableState = {
    entities: [],
    lastAdd: null,
    lastRemove: null,
    visible: 'hide',
    selectedIndex: 0,
    mouseOver: null,
    mouseOut: null
}

export function dataTableReducer(state: DataTableState = initialDataTableState, action: DataTableAction): DataTableState {
    switch(action.type) {
        case DataTableActionTypes.DATA_TABLE_ADD: {
            const payload: DataTable = {...action.payload};
            let entities: DataTable[] = [...state.entities];
            let index = entities.findIndex(item => item.id === payload.id);
            let selectedIndex: number = 0;
            if(index === -1) {
                entities.push(payload);
                selectedIndex = entities.length -1;
            } else {
                selectedIndex = index;
            }
            return {
                ...state,
                entities,
                lastAdd: payload,
                selectedIndex,
                visible: entities.length > 0 ? 'show' : 'hide',
            }
        }
        case DataTableActionTypes.DATA_TABLE_REMOVE: {
            const id: string = action.payload;
            let entities: DataTable[] = [...state.entities];
            const index: number = entities.findIndex(item => item.id === id);
            if(index > -1) {
                entities.splice(index, 1);
            }
            return {
                ...state,
                entities,
                lastRemove: entities[id],
                visible: entities.length > 0 ? 'show' : 'hide'
            }
        }
        case DataTableActionTypes.DATA_TABLE_SHOW: {
            return {
                ...state,
                visible: 'show'
            }
        }
        case DataTableActionTypes.DATA_TABLE_HIDE: {
            return {
                ...state,
                visible: 'hide'
            }
        }
        case DataTableActionTypes.DATA_TABLE_SELECTED: {
            return {
                ...state,
                selectedIndex: action.payload
            };
        }
        case DataTableActionTypes.DATA_TABLE_DOWN: {
            return {
                ...state,
                visible: 'half-hide'
            }
        }
        case DataTableActionTypes.DATA_TABLE_MOUSE_OVER: {
            return {
                ...state,
                mouseOver: action.payload,
                mouseOut: null
            }
        }
        case DataTableActionTypes.DATA_TABLE_MOUSE_OUT: {
            return {
                ...state,
                mouseOver: null,
                mouseOut: action.payload
            }
        }
        case DataTableActionTypes.DATA_TABLE_DATA: {
            const payload = action.payload;
            const id = payload.id;
            const data = payload.data;
            const entities = state.entities.map((table: DataTable) => {
                if(table.id === id) {
                    table = {
                        ...table,
                        data
                    };
                }
                return table;
            });
            return {
                ...state,
                entities
            };
        }
        case DataTableActionTypes.DATA_TABLE_LOADING: {
            const id = action.payload;
            const entities = state.entities.map((table: DataTable) => {
                if(table.id === id) {
                    table = {
                        ...table,
                        loaded: false,
                        loadError: false,
                        loading: true
                    };
                }
                return table;
            });
            return {
                ...state,
                entities
            };
        }
        case DataTableActionTypes.DATA_TABLE_LOADED: {
            const id = action.payload;
            const entities = state.entities.map((table: DataTable) => {
                if(table.id === id) {
                    table = {
                        ...table,
                        loaded: true,
                        loadError: false,
                        loading: false
                    };
                }
                return table;
            });
            return {
                ...state,
                entities
            };
        }
        case DataTableActionTypes.DATA_TABLE_LOAD_ERROR: {
            const id = action.payload;
            const entities = state.entities.map((table: DataTable) => {
                if(table.id === id) {
                    table = {
                        ...table,
                        loaded: false,
                        loadError: true,
                        loading: false
                    };
                }
                return table;
            });
            return {
                ...state,
                entities
            };
        }
        case DataTableActionTypes.DATA_TABLE_RESET: {
            return initialDataTableState;
        }
        default: {
            return state;
        }
    }
}